package de.guidoludwig.jatt.google;

import java.awt.Component;
import java.awt.FlowLayout;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

final class GoogleImageInfoListCellRenderer implements ListCellRenderer<GoogleImageInfo> {
	
	private JLabel icon;
	private JLabel text;
	private JPanel component;
	
	public GoogleImageInfoListCellRenderer() {
		icon = new JLabel();
		text = new JLabel("");
		text.setOpaque(false);
		component = new JPanel(new FlowLayout(FlowLayout.LEFT));
		component.add(icon);
		component.add(text);
	}

	@Override
	public Component getListCellRendererComponent(
			JList<? extends GoogleImageInfo> list, GoogleImageInfo value,
			int index, boolean isSelected, boolean cellHasFocus) {
		try {
			icon.setIcon(new ImageIcon(value.getThumb()));
		} catch (IOException e) {
			icon.setIcon(null);
		}
		text.setText(value.getWidth() + "x" + value.getHeight() + "px");
		component.setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
		return component;
	}
	
}